package com.example.newsapp.data.database

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.Query
import androidx.room.Update

@Dao
interface NewsDao {
    // 添加数据
    @Insert
    suspend fun insert(newsItems: NewsItems)

    // 修改数据
    @Update
    suspend fun update(newsItems: NewsItems)

    // 删除数据
    @Delete
    suspend fun delete(newsItems: NewsItems)

    // 根据id删除数据
    @Query("delete from news_items where id = :id")
    suspend fun deleteById(id: Int)

    // 根据num查询新闻
    @Query("select * from news_items where num = :num")
    suspend fun selectByNum(num: Int): NewsItems

    // 查询所有热点新闻
    @Query("select * from news_items where type = 'hot'")
    suspend fun selectHotNews(): MutableList<NewsItems>

    // 获取热点新闻数
    @Query("select count(*) from news_items where type = 'hot'")
    suspend fun selectHotNewsNums(): Int

    // 查询所有NBA新闻
    @Query("select * from news_items where type = 'NBA'")
    suspend fun selectNBANews(): MutableList<NewsItems>

    // 获取NBA新闻数
    @Query("select count(*) from news_items where type = 'NBA'")
    suspend fun selectNBANewsNums(): Int

    // 查询所有汽车新闻
    @Query("select * from news_items where type = 'car'")
    suspend fun selectCarNews(): MutableList<NewsItems>

    // 获取汽车新闻数
    @Query("select count(*) from news_items where type = 'car'")
    suspend fun selectCarNewsNums(): Int

    // 查询所有笑话新闻
    @Query("select * from news_items where type = 'joke'")
    suspend fun selectJokeNews(): MutableList<NewsItems>

    // 获取汽车新闻数
    @Query("select count(*) from news_items where type = 'joke'")
    suspend fun selectJokeNewsNums(): Int

    // 查询所有国际新闻
    @Query("select * from news_items where type = 'internation'")
    suspend fun selectInternationNews(): MutableList<NewsItems>

    // 获取国际新闻数
    @Query("select count(*) from news_items where type = 'internation'")
    suspend fun selectInternatioNewsNums(): Int

    // 获取所有新闻数
    @Query("select count(*) from news_items where num != 0")
    suspend fun selectAllNewsNums(): Int

    // 根据title模糊查询
    @Query("select * from news_items where title like :title and num != 0")
    suspend fun selectByTitle(title: String): List<NewsItems>
}